layui.use(['jquery', 'form', 'common', 'tableSelect', 'numinput', 'laydate'], function() {
	var $ = layui.jquery;
	var form = layui.form;
	var common = layui.common;
	var tableSelect = layui.tableSelect;
	var numinput = layui.numinput;
	var laydate = layui.laydate;

	form.on('submit(form)', function(data) {
		$.post('save', data.field, function(data) {
			if (data.state == 'ok') {
				// 成功，跳转到列表页面
				common.toUrl('list');
			} else {
				// 失败，弹出提示
				layer.msg(data.msg, {
					icon: 5,
					anim: 6
				});
			}
		});
		return false;
	});
	tableSelect.render({
		elem: '#servantId',
		checkedKey: 'id',
		table: {
			url: 'getServants',
			page: true,
			skin: 'line',
			request: {
				pageName: 'current',
				limitName: 'size'
			},
			cols: [[
				{type: 'radio'},
				{field: 'gameId', title: '编号', rowspan: 2, sort: true, templet: '<div>No.{{d.gameId}}</div>'},
				{field: 'avatar', title: '头像', rowspan: 2, templet: common.tableImageTemplet('avatar', 65, 'name')},
				{field: 'name', title: '姓名', rowspan: 2, minWidth: 250}
			]],
			autoSort: false,
			initSort: {
				field: 'gameId',
				type: 'asc'
			},
			where: {
				orderByField: 'gameId',
				isAsc: true
			}
		},
		done: function (elem, data) {
			if (data.data.length) {
				elem.val(data.data[0].name);
				$('[name="servantId"]').val(data.data[0].id);
			}
		}
	});
	numinput.init({
		topBtns: 789
	});
	// 禁用退格键后退
	common.forbidBackSpace();
	laydate.render({
		elem: '[name="getDate"]',
		type: 'date',
		max: 0
	});
});